
$(function(){
	$("#workOrderNo").val(getUrlParam("workOrderNo"));
	$("#productName").val(decodeURI(getUrlParam("productName")));
});

$(".layui-form-radio").click(function(){
	$("#repairMethod").val($(this).children(".radio-left").text());
	$("#repairMethod").attr("val",$(this).children(".radio-left").attr("val"));
});

$("#sparepartFee").click(function(){
	var sparepartFee = 0;
	if ($("#pro").children(".idm").length != 0) {
		$.each($("#pro").children(".idm"), function(index,elem) {
			var	money = $(elem).children(".money").attr("val");
			sparepartFee = parseFloat(money) + parseFloat(sparepartFee)
		});
	}
	console.log(sparepartFee);
	$(this).val(sparepartFee);
});


var repairRecordId = getUrlParam("repairRecordId");

layui.use('upload', function () {
    var $ = layui.jquery,
        upload = layui.upload;
   
	//多图上传
	upload.render({
		elem: '#uploadpic',
		method: 'post',
		auto: false, //不自动上传
		multiple: true,
		choose: function (obj) {
			obj.preview(function (index, file, result) {
				var url = URL.createObjectURL(file);
				var curTiem = new Date().getTime();
				$("#picsPlace").append('<div class="zoomImage" style="background-image:url(' + url + ')" title="'+(curTiem + file.name)+'"></div>');
				var param = {
					file:file,
					success:function(err, data){
						if (err) {
							layer.msg('上传失败！');
							return;
						}
					},
					curTiem:curTiem
				};
				uploadToCos(param);
			});
		}
	});
	//	多视频上传
	var uploadInst = upload.render({
		elem: '#uploadVideo',
		method: 'post',
		accept: 'video', //视频类型
		exts:'mov|mp4',
		auto: false,
		multiple: true,
		choose: function (obj) { // 选择文件之后，提交文件之前，你想干什么？
			var loadIndex = layer.load(2);
			obj.preview(function (index, file, result) {
				// var curTiem = new Date().getTime();
				var url = URL.createObjectURL(file)
				var param = {
					file: file,
					finish: function(result){//上传成功时的回调函数
						console.log(result);
						console.log('上传结果的fileId：' + result.fileId);
						console.log('上传结果的视频名称：' + result.videoName);
						console.log('上传结果的视频地址：' + result.videoUrl);
						if (url != null) { // 显示video标签，用于预览
							$('.video').css("display", "inline-block");
							$('#videoPlay1').attr('src', url); //预读本地文件示例，

							var videoDom = ['<div class="videoDiv">',
							'<video class="prev" src="' + url + '" title="'+file.name+'" fileId="'+ result.fileId +'" x-webkit-airplay="true" ',
							'autoplay webkit-playsinline="true" object-fit: fill></video>',
							'<a href="javascript:" onclick="delVidoe()" ><img class="delVideo" src="../../plugin/rewrite/img/delVideo.png"/></a></div>'].join('');
							$("#videoPlace").append(videoDom);
							layer.close(loadIndex);
						}
					}
				}
				uploadToVod(param);
			})
		}
	});
	
	$('#submitFeedBack').on('click', function(){
		$.each($('#picsPlace div'), function(index, elem){
			console.log(elem);
		});

		var loadIndex = layer.load(2);
		//判断是否为空
		var emptity = 0;
		var feedBackInfo = {};
		var sparepartFees = []; //零配件费用
		var repairFiles = []; //保存图片和视频地址
		
		
		$.each($('input[type=text],textarea'), function(index, elem){
			
			if (elem.id === "repairMethod") {
				console.log(elem);
				feedBackInfo[elem.id] = $(elem).attr("val");
			} else {
				feedBackInfo[elem.id] = elem.value;
			}
			 
		});
	/**
     * 保存维修信息
     * @param id 维修记录id
     * @param feedBackInfo 维修人员填写的信息
     * @param sparepartFees 零配件费用(null/sparepartFee对象数组)
     * @param repairFiles 问题描述的文件(图片/视频)(null/repireFile对象数组)
     * @return
     */
		if(emptity === 0){

			//所需零备件费用及条码
			if ($("#pro").children(".idm").length != 0) {
				$.each($("#pro").children(".idm"), function(index,elem) {
					var sparepartFee = {
						sparepartName:$(elem).children(".product").text(),
						money:$(elem).children(".money").attr("val"),
						oldBarCode:$(elem).find(".old>span").text(),
						newBarCode:$(elem).find(".new>span").text()
					}
					sparepartFees.push(sparepartFee);
				});
			}

			//遍历获取上传的图片和视频
			$.each($('#picsPlace div'), function(index, elem){
				var image = {
					fileName:elem.title,
					fileType:REPAIR_FILE_TYPE_PICTURE,
					userType:SOURCE_REPAIR
				}
				repairFiles.push(image);
			});

			$.each($('#videoPlace video'), function(index, elem){
				var video = {
					fileName:elem.title,
					fileId:$(elem).attr('fileid'),
					appId:VOD_APP_ID,
					fileType:REPAIR_FILE_TYPE_VIDEO,
					userType:SOURCE_REPAIR
				}
				repairFiles.push(video);
			});

			//该工单的创建人员类型(1:客户/2:维修人员)
			feedBackInfo.source = SOURCE_REPAIR;

			Server.postToService('/saveFeedBackInfo', {id:repairRecordId,feedBackInfo: JSON.stringify(feedBackInfo),sparepartFees:JSON.stringify(sparepartFees),repairFiles:JSON.stringify(repairFiles)}, function(result){
				if(result.code === 200){
					layer.msg("提交成功!");
				}else{
					layer.msg("提交失败！");
				}
				layer.close(loadIndex);
			});
		} else {
			layer.close(loadIndex);
		}
		
	});

});
//删除视频
function delVidoe() {
    console.log('删除');
    console.log($(event.target))
    $(event.target).parent().parent().remove()
}

//浏览删除图片
$(function () {
    var $gallery = $("#gallery"),
        $galleryImg = $("#galleryImg");
    var index; //第几张图片
    $("#picsPlace").on("click", "div", function () {
        index = $(this).index();
        $galleryImg.attr("style", this.getAttribute("style"));
        $gallery.fadeIn(100);
    });
    $gallery.on("click", function () {
        $gallery.fadeOut(100);
    });
    //删除图片 删除图片的代码也贴出来。
    $(".weui-gallery__del").click(function () {
        console.log('删除');
        $("#picsPlace").find("div").eq(index).remove();
    });
});

// 上传视频图片弹出框
$("#uploadpic").click(function () {
    $("#uploadPicVideo").slideToggle();
    $(".mui-backdrop").css("display", "none");
});
$("#uploadVideo").click(function () {
    $("#uploadPicVideo").slideToggle();
    $(".mui-backdrop").css("display", "none");
});


